/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exception.DAOConnectionException;
import dao.exception.DAOPersonneException;
import dao.interfaces.DAOPersonne;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import model.Personne;

/**
 *
 * @author Paul
 */
public class DAOPersonneImpl implements DAOPersonne {

    private DAOInit daoConnection;

    public DAOPersonneImpl(DAOInit daoConnection) {
        this.daoConnection = daoConnection;
    }

    public Personne rechercheUtilisateur(String login, String mdp) throws DAOPersonneException, DAOConnectionException{
        /*
         * Base de données
         */
        PreparedStatement st = null;
        Personne p = null;
        try {
            Connection conn = daoConnection.getConnection();
            String query = "select * from personne where login='" + login + "' and password='" + mdp + "';";
            st = conn.prepareStatement(query);
            ResultSet res = st.executeQuery(query);
            if (res.next()) {
                String log = res.getString("login");
                String password = res.getString("password");
                String nom = res.getString("nom");
                String prenom = res.getString("prenom");
                String type = res.getString("typeUtilisateur");
                int id = 0;
                Date d = null;
                p = new Personne(id, nom, prenom, d, type, log, password);
            }
            else {
                throw new DAOPersonneException("Vous n'êtes pas inscris sur l'application");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return p;
    }
}
